package com.binya.lib.permission.share;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

/**
 * 基础实现
 */
public class BaseSharePreference {

    private SharedPreferences sp;

    protected BaseSharePreference(Context context, String spName) {
        this.sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
    }

    /**
     * @param key 键
     * @return 更据键获取对应的值
     */
    public String getString(String key) {
        return getString(key, "");
    }

    public String getString(String key, String vel) {
        return get(key, vel);
    }

    public long getLong(String key) {
        return getLong(key, 0L);
    }

    public long getLong(String key, Long l) {
        return get(key, l);
    }

    public int getInt(String key) {
        return getInt(key, 0);
    }

    public int getInt(String key, int vel) {
        return get(key, vel);
    }

    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean vel) {
        return get(key, vel);
    }

    public float getFloat(String key) {
        return getFloat(key, 0F);
    }

    public float getFloat(String key, Float f) {
        return get(key, f);
    }

    public <T> T get(String key) {
        Object o = sp.getAll().get(key);
        if (o == null) {
            o = "";
        }
        return (T) o;
    }

    public <T> T get(String key, Object o) {
        Object o1 = sp.getAll().get(key);
        if (o1 == null) {
            o1 = o;
        }
        return (T) o1;
    }

    /**
     * 数据保存（异步）
     *
     * @param key     键
     * @param content 值
     */
    public void set(String key, Object content) {
        try {
            if (content instanceof String) {
                setString(key, (String) content);
            } else if (content instanceof Long) {
                setLong(key, (Long) content);
            } else if (content instanceof Integer) {
                setInt(key, (Integer) content);
            } else if (content instanceof Boolean) {
                setBoolean(key, (Boolean) content);
            } else if (content instanceof Float) {
                setFloat(key, (Float) content);
            }
        } catch (Exception e) {
            Log.e("SP", "类型转换错误");
        }
    }

    public void setString(String key, String content) {
        sp.edit().putString(key, content).apply();
    }

    public void setLong(String key, Long l) {
        sp.edit().putLong(key, l).apply();
    }

    public void setInt(String key, int content) {
        sp.edit().putInt(key, content).apply();
    }

    public void setBoolean(String key, boolean status) {
        sp.edit().putBoolean(key, status).apply();
    }

    public void setFloat(String key, float f) {
        sp.edit().putFloat(key, f).apply();
    }

    /**
     * 数据保存 (同步)
     *
     * @param key     键
     * @param content 值
     */
    public boolean setCommit(String key, Object content) {
        try {
            if (content instanceof String) {
                return setStringCommit(key, (String) content);
            } else if (content instanceof Long) {
            } else if (content instanceof Integer) {
                return setIntCommit(key, (Integer) content);
            } else if (content instanceof Boolean) {
                return setBooleanCommit(key, (Boolean) content);
            } else if (content instanceof Float) {
                return setFloatCommit(key, (Float) content);
            }
        } catch (Exception e) {
            Log.e("SP", "类型转换错误");
        }
        return false;
    }


    public boolean setStringCommit(String key, String content) {
        return sp.edit().putString(key, content).commit();
    }

    public boolean setLongCommit(String key, Long l) {
        return sp.edit().putLong(key, l).commit();
    }

    public boolean setIntCommit(String key, int content) {
        return sp.edit().putInt(key, content).commit();
    }

    public boolean setBooleanCommit(String key, boolean status) {
        return sp.edit().putBoolean(key, status).commit();
    }

    public boolean setFloatCommit(String key, float f) {
        return sp.edit().putFloat(key, f).commit();
    }

    public boolean clearInfo() {
        return sp.edit().clear().commit();
    }
}
